package org.googlecode.viewt.core.tools.po;

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;
import org.googlecode.viewt.core.tools.po.types.BagChoice;
import org.googlecode.viewt.core.tools.po.types.BeanPropertyDef;
import org.googlecode.viewt.core.tools.po.types.ComponentDef;
import org.googlecode.viewt.core.tools.po.types.EmbedObjectDef;
import org.googlecode.viewt.core.tools.po.types.EmbedObjectItem;
import org.googlecode.viewt.core.tools.po.types.EmbedQueryItem;
import org.googlecode.viewt.core.tools.po.types.Import;
import org.googlecode.viewt.core.tools.po.types.ListChoice;
import org.googlecode.viewt.core.tools.po.types.ManyOneAssociationDef;
import org.googlecode.viewt.core.tools.po.types.MapChoice;
import org.googlecode.viewt.core.tools.po.types.OneOneAssociationDef;
import org.googlecode.viewt.core.tools.po.types.PersistObjectDescDef;
import org.googlecode.viewt.core.tools.po.types.PersistObjects;
import org.googlecode.viewt.core.tools.po.types.PropertyMappingChoice;
import org.googlecode.viewt.core.tools.po.types.PropertyMappingDef;
import org.googlecode.viewt.core.tools.po.types.QueryCondition;
import org.googlecode.viewt.core.tools.po.types.QueryMethod;
import org.googlecode.viewt.core.tools.po.types.SetChoice;
import org.googlecode.viewt.core.utils.TypeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class POConfigUtil {

	private static Logger logger = LoggerFactory.getLogger(POConfigUtil.class);

	public static Map primaryMap = null;
	public static Map collectionMap = null;

	public static String[] primaryNames = { "boolean", "char", "byte", "short",
			"int", "long", "float", "double" };

	public static BeanPropertyDef propDefByName(String propName,
			BeanPropertyDef[] propDefs) {
		if ((null == propName) || ("".equals(propName))) {
			return null;
		}
		for (int i = 0; i < propDefs.length; ++i) {
			if (propName.equals(propDefs[i].getName())) {
				return propDefs[i];
			}
		}
		return null;
	}

	public static PersistObjects loadConfig(String cfgFile, String prePackName) {
		String curModule;
		String curProject;
		String curPOPackageName;
		
		if ((null == cfgFile) || ("".equals(cfgFile))) {
			logger.error("Invalid file name, config file must specified");
			return null;
		}
		
		
		FileReader reader = null;
		try {
			String className;
			reader = new FileReader(cfgFile);
			PersistObjects pos = PersistObjects.unmarshal(reader);

			curModule = pos.getModuleName().toLowerCase();
			pos.setModuleName(curModule);
			curProject = pos.getProjectName().toLowerCase();
			pos.setProjectName(curProject);

			curPOPackageName = poPackageName(prePackName, curProject, curModule);
			
			
			PersistObjectDescDef[] poDefs = pos.getPersistObjectDescDef();
			for (int i = 0; i < poDefs.length; ++i) {
				ListChoice listChoice;
				className = poDefs[i].getClassName();
				if (className.indexOf(46) >= 0) {
					if (isClassInPackage(className, curPOPackageName))
						logger
								.error("Invalid PO class name, must within package:"
										+ curPOPackageName);
					return null;
				}
				
				logger.debug("classsname:  " + className);
				
				poDefs[i].setClassName(toFullClassName(className,
						curPOPackageName, false));

				String parentClassName = poDefs[i].getParentClassName();
				
				if ((parentClassName != null)
						&& (!("".equals(parentClassName)))) {
					String fullName = toFullClassName(parentClassName,
							curPOPackageName, false);
					poDefs[i].setParentClassName(fullName);
					addClassImport(poDefs[i], fullName, curPOPackageName);
				}
				

				BeanPropertyDef[] propDefs = poDefs[i].getBeanPropertyDef();
				for (int j = 0; j < propDefs.length; ++j) {
					
					String fullName;

					String type = propDefs[j].getType();
					String itemType = propDefs[j].getItemType();
					String keyType = propDefs[j].getKeyType();
					
					if (isCollectionType(type)) {
						if ((itemType == null) || ("".equals(itemType))) {
							logger.error("Item type of collection property:"
									+ propDefs[j].getName()
									+ " must be specified");
							return null;
						}

						if (isMapCollectionType(type)) {
							if ((keyType == null) || ("".equals(keyType))) {
								logger
										.error("Key type of map collection property:"
												+ propDefs[j].getName()
												+ " must be specified");
								return null;
							}

							fullName = toFullClassName(keyType,
									curPOPackageName, true);
							propDefs[j].setKeyType(fullName);
							addClassImport(poDefs[i], fullName,
									curPOPackageName);
						}

						fullName = toFullClassName(type, curPOPackageName,
								false);
						propDefs[j].setType(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);

						fullName = toFullClassName(itemType, curPOPackageName,
								true);
						
						propDefs[j].setItemType(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					} else {
						fullName = toFullClassName(type, curPOPackageName,
								false);
						
						propDefs[j].setType(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					PropertyMappingDef mapDef = propDefs[j]
							.getPropertyMappingDef();
					if (mapDef == null)
						continue;
					PropertyMappingChoice mapChoice = mapDef
							.getPropertyMappingChoice();
					if (mapChoice == null) {
						continue;
					}
					ComponentDef compDef = mapChoice.getComponentDef();
					if ((compDef != null) && (compDef.getClazz() != null)) {
						fullName = toFullClassName(compDef.getClazz(),
								curPOPackageName, false);
						compDef.setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					OneOneAssociationDef oneOneDef = mapChoice
							.getOneOneAssociationDef();
					if ((oneOneDef != null) && (oneOneDef.getClazz() != null)) {
						fullName = toFullClassName(oneOneDef.getClazz(),
								curPOPackageName, false);
						oneOneDef.setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					ManyOneAssociationDef manyOneDef = mapChoice
							.getManyOneAssociationDef();
					if ((manyOneDef != null) && (manyOneDef.getClazz() != null)) {
						fullName = toFullClassName(manyOneDef.getClazz(),
								curPOPackageName, false);
						manyOneDef.setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					BagChoice bagChoice = (mapChoice.getBagDef() == null) ? null
							: mapChoice.getBagDef().getBagChoice();
					if ((bagChoice != null)
							&& (bagChoice.getCompElement() != null)
							&& (bagChoice.getCompElement().getClazz() != null)) {
						fullName = toFullClassName(bagChoice.getCompElement()
								.getClazz(), curPOPackageName, false);
						bagChoice.getCompElement().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}
					if ((bagChoice != null) && (bagChoice.getOneMany() != null)
							&& (bagChoice.getOneMany().getClazz() != null)) {
						fullName = toFullClassName(bagChoice.getOneMany()
								.getClazz(), curPOPackageName, false);
						bagChoice.getOneMany().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					listChoice = (mapChoice.getListDef() == null) ? null
							: mapChoice.getListDef().getListChoice();
					if ((listChoice != null)
							&& (listChoice.getCompElement() != null)
							&& (listChoice.getCompElement().getClazz() != null)) {
						fullName = toFullClassName(listChoice.getCompElement()
								.getClazz(), curPOPackageName, false);
						listChoice.getCompElement().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}
					if ((listChoice != null)
							&& (listChoice.getOneMany() != null)
							&& (listChoice.getOneMany().getClazz() != null)) {
						fullName = toFullClassName(listChoice.getOneMany()
								.getClazz(), curPOPackageName, false);
						listChoice.getOneMany().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					MapChoice mChoice = (mapChoice.getMapDef() == null) ? null
							: mapChoice.getMapDef().getMapChoice();
					if ((mChoice != null) && (mChoice.getCompElement() != null)
							&& (mChoice.getCompElement().getClazz() != null)) {
						fullName = toFullClassName(mChoice.getCompElement()
								.getClazz(), curPOPackageName, false);
						mChoice.getCompElement().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}
					if ((mChoice != null) && (mChoice.getOneMany() != null)
							&& (mChoice.getOneMany().getClazz() != null)) {
						fullName = toFullClassName(mChoice.getOneMany()
								.getClazz(), curPOPackageName, false);
						mChoice.getOneMany().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}

					SetChoice setChoice = (mapChoice.getSetDef() == null) ? null
							: mapChoice.getSetDef().getSetChoice();
					if ((setChoice != null)
							&& (setChoice.getCompElement() != null)
							&& (setChoice.getCompElement().getClazz() != null)) {
						fullName = toFullClassName(setChoice.getCompElement()
								.getClazz(), curPOPackageName, false);
						setChoice.getCompElement().setClazz(fullName);
						addClassImport(poDefs[i], fullName, curPOPackageName);
					}
					if ((setChoice == null) || (setChoice.getOneMany() == null)
							|| (setChoice.getOneMany().getClazz() == null))
						continue;
					fullName = toFullClassName(setChoice.getOneMany()
							.getClazz(), curPOPackageName, false);
					setChoice.getOneMany().setClazz(fullName);
					addClassImport(poDefs[i], fullName, curPOPackageName);
				}

				if (poDefs[i].getEmbedObjectDef() == null)
					continue;
				EmbedObjectItem[] embedItems = poDefs[i].getEmbedObjectDef()
						.getEmbedObjectItem();
				for (int j = 0; j < embedItems.length; ++j) {
					QueryMethod queryMethod = embedItems[j].getQueryMethod();
					String methodName = queryMethod.getMethodName();
					EmbedQueryItem[] queryItems = queryMethod
							.getEmbedQueryItem();
					for (int k = 0; k < queryItems.length; ++k) {
						QueryCondition condition = queryItems[k]
								.getQueryCondition();

						BeanPropertyDef propDef = propDefByName(condition
								.getPropertyName(), propDefs);
						if (null == propDef) {
							logger.error("Specified property:"
									+ condition.getPropertyName()
									+ " for embed query:" + methodName
									+ " not found");
							return null;
						}

						String type = propDef.getType();
						if ((!(primaryMap.containsKey(type)))
								&& (!("java.util.Date".equals(type)))) {
							logger.error("Data type:" + type + " of property:"
									+ condition.getPropertyName()
									+ " for embed query:" + methodName
									+ " is not supported");
							return null;
						}

						//FIXME
//						switch (condition.getCondition().getType()) {
//						case 0:
//							break;
//						case 1:
//							if ("java.lang.String".equals(type))
//								continue;
//							logger
//									.error("Data type mismatch with condition, check property:"
//											+ condition.getPropertyName()
//											+ " of embed query:" + methodName);
//							return null;
//						case 2:
//						case 3:
//						case 4:
//						case 5:
//						case 6:
//							if ((!("java.lang.String".equals(type)))
//									&& (!("boolean".equals(type)))
//									&& (!("java.lang.Boolean".equals(type))))
//								continue;
//							logger
//									.error("Data type mismatch with condition, check property:"
//											+ condition.getPropertyName()
//											+ " of embed query:" + methodName);
//							return null;
//						}

					}

				}

			}

			return pos;
		} catch (FileNotFoundException fnfEx) {
			logger.error("Specified config file:" + cfgFile + " not found");
			return null;
		} catch (MarshalException mEx) {
			logger.error("Unmarshal the config file:" + cfgFile + " fail", mEx);
			return null;
		} catch (ValidationException vEx) {
			logger.error("Validate the config file:" + cfgFile + " fail", vEx);
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException ioe) {
				}
			}
		}
	}

	public static String poPackageName(String prePackName, String projectName,
			String moduleName) {
		return prePackName + "." + projectName + "." + moduleName + ".po";
	}

	public static boolean isBasicPrimaryType(String typeName) {
		for (int i = 0; i < primaryNames.length; ++i) {
			if (primaryNames[i].equals(typeName)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isPrimaryObjectType(String typeName) {
		if (isBasicPrimaryType(typeName)) {
			return false;
		}

		return (!(primaryMap.containsKey(typeName)));
	}

	public static boolean isClassPrimary(String className) {
		className = getTypeInArray(className);

		return primaryMap.containsKey(className);
	}

	public static boolean isClassInPackage(String className, String packageName) {
		String remain;
		className = getTypeInArray(className);
		StringBuffer sb = new StringBuffer(packageName);
		sb.append(".");

		if (className.startsWith(sb.toString())) {
			remain = className.substring(packageName.length() + 1);
			if (remain.indexOf(46) < 0) {
				return true;
			}
		}
		return false;
	}

	public static void addClassImport(PersistObjectDescDef poDef,
			String className, String curPackage) {
		className = getTypeInArray(className);

		if (isClassPrimary(className)) {
			logger.debug("class:" + className
					+ " is primary type, need not import");
			return;
		}

		if (isClassInPackage(className, curPackage)) {
			logger.debug("class:" + className
					+ " is within current PO package, need not import");
			return;
		}

		Import[] imports = poDef.getImport();
		for (int i = 0; i < imports.length; ++i) {
			if (imports[i].getClazz().equals(className)) {
				return;
			}
		}
		Import classImport = new Import();
		classImport.setClazz(className);
		poDef.addImport(classImport);
	}

	public static String collectionImplType(String typeName) {
		return ((String) collectionMap.get(typeName));
	}

	public static boolean isCollectionType(String typeName) {
		
		return (("map".equalsIgnoreCase(typeName))
				|| ("set".equalsIgnoreCase(typeName))
				|| ("list".equalsIgnoreCase(typeName))
				|| ("java.util.Map".equals(typeName))
				|| ("java.util.Set".equals(typeName)))
				|| ("java.util.List".equals(typeName));
	}

	public static boolean isMapCollectionType(String typeName) {
		return ("map".equalsIgnoreCase(typeName)) || ("java.util.Map"
				.equals(typeName));
	}

	public static boolean isSetCollectionType(String typeName) {
		return ("set".equalsIgnoreCase(typeName)) || ("java.util.Set"
				.equals(typeName));
	}

	public static boolean isListCollectionType(String typeName) {
		return ("list".equalsIgnoreCase(typeName)) || ("java.util.List"
				.equals(typeName));
	}

	public static boolean isArrayType(String typeName) {
//		return (!(typeName.endsWith("[]")));
		return typeName.endsWith("[]");
	}

	public static boolean isObjectArrayType(String typeName) {
		if (!(isArrayType(typeName))) {
			return false;
		}
		String className = getTypeInArray(typeName);
		for (int i = 0; i < primaryNames.length; ++i) {
			if (primaryNames[i].equals(className)) {
				return false;
			}
		}
		return true;
	}

	public static String getTypeInArray(String typeName) {
		if (isArrayType(typeName)) {
			return typeName.substring(0, typeName.length() - 2);
		}
		return typeName;
	}

	public static String shortTypeInArray(String typeName) {
		return shortClassName(getTypeInArray(typeName));
	}

	public static String poApiTypeInArray(String typeName,
			POConfigRepository poRepository) {
		return poApiClassName(getTypeInArray(typeName), poRepository);
	}

	public static String poShortApiTypeInArray(String typeName,
			POConfigRepository poRepository) {
		return shortClassName(poApiTypeInArray(typeName, poRepository));
	}

	public static String firstUpper(String propName) {
		return propName.substring(0, 1).toUpperCase() + propName.substring(1);
	}

	public static String toFullClassName(String typeName, String curPackage,
			boolean convertPrim) {
		
		String fullName = null;
		
		if ("map".equalsIgnoreCase(typeName))
			return "java.util.Map";
		if ("set".equalsIgnoreCase(typeName))
			return "java.util.Set";
		if ("list".equalsIgnoreCase(typeName)) {
			return "java.util.List";
		}

		boolean arrayType = false;
		
		
		if (isArrayType(typeName)) {
			typeName = getTypeInArray(typeName);
			arrayType = true;
		}
		
		if (isClassPrimary(typeName)) {
			if (convertPrim)
				fullName = (String) primaryMap.get(typeName);
			else {
				fullName = TypeUtil.fullPrimaryClassName(typeName);
			}

		} else if (typeName.indexOf(46) < 0){
			fullName = curPackage + "." + typeName;
		}else {
			fullName = typeName;
		}

		if (arrayType) {
			fullName = fullName + "[]";
		}
		return fullName;
	}

	public static String getPOModule(String className) {
		if ((className == null) || ("".equals(className))) {
			return null;
		}
		int index = className.lastIndexOf(".po.");
		if (index < 0)
			return null;
		String moduleClassName = className.substring(0, index);
		index = moduleClassName.lastIndexOf(46);
		if (index < 0) {
			return null;
		}
		return moduleClassName.substring(index + 1);
	}

	public static String getPOProject(String className) {
		if ((className == null) || ("".equals(className))) {
			return null;
		}
		int index = className.lastIndexOf(".po.");
		if (index < 0)
			return null;
		String moduleClassName = className.substring(0, index);
		index = moduleClassName.lastIndexOf(46);
		if (index < 0)
			return null;
		String projectClassName = moduleClassName.substring(0, index);
		index = projectClassName.lastIndexOf(46);
		if (index < 0) {
			return null;
		}
		return projectClassName.substring(index + 1);
	}

	public static String classVariable(String className) {
		String shortName = shortTypeInArray(className);
		return shortName.substring(0, 1).toLowerCase() + shortName.substring(1);
	}

	public static String shortClassName(String className) {
		return className.substring(className.lastIndexOf(46) + 1);
	}

	public static String shortApiClassName(String className) {
		return "I" + shortClassName(className);
	}

	public static String apiClassName(String className) {
		int index = className.lastIndexOf(46);
		if (index < 0) {
			return "I" + className;
		}
		return className.substring(0, index + 1) + "I"
				+ className.substring(index + 1);
	}

	public static String shortEmbedApiClassName(String className) {
		return "IEmbed" + shortClassName(className);
	}

	public static String embedApiClassName(String className) {
		int index = className.lastIndexOf(46);
		if (index < 0) {
			return "IEmbed" + className;
		}
		return className.substring(0, index + 1) + "IEmbed"
				+ className.substring(index + 1);
	}

	public static String shortEmbedImpClassName(String className) {
		return "Embed" + shortClassName(className);
	}

	public static String embedImpClassName(String className) {
		int index = className.lastIndexOf(46);
		if (index < 0) {
			return "Embed" + className;
		}
		return className.substring(0, index + 1) + "Embed"
				+ className.substring(index + 1);
	}

	public static String poApiClassName(String className,
			POConfigRepository poRepository) {
		if (poRepository.getPOConfig(className) != null) {
			return apiClassName(className);
		}
		return className;
	}

	public static String poShortApiClassName(String className,
			POConfigRepository poRepository) {
		return shortClassName(poApiClassName(className, poRepository));
	}

	public static String descriptorClassName(String className) {
		return className + "Descriptor";
	}

	public static String shortDescriptorClassName(String className) {
		return shortClassName(className) + "Descriptor";
	}

	public static String getPackageName(String className) {
		int index = className.lastIndexOf(46);
		if (index < 0) {
			return null;
		}
		return className.substring(0, index);
	}

	public static String getNamespaceURI(String prePackName, String className) {
		String relativePackage = getPackageName(className).substring(
				prePackName.length() + 1);

		String nsURI = "http://www";
		String[] hostSeg = prePackName.split("[.]");
		for (int i = hostSeg.length; i > 0; --i) {
			nsURI = nsURI + "." + hostSeg[(i - 1)];
		}

		String[] packageSeg = relativePackage.split("[.]");
		for (int i = 0; i < packageSeg.length; ++i) {
			nsURI = nsURI + "/" + packageSeg[i];
		}
		return nsURI;
	}

	public static String getPackagePath(String basePath, String className) {
		String[] packageSeg = getPackageName(className).split("[.]");
		String path = "";
		for (int i = 0; i < packageSeg.length; ++i) {
			if (i != 0)
				path = path + File.separator;
			path = path + packageSeg[i];
		}

		if (basePath != null) {
			path = basePath + File.separator + path;
		}
		return path;
	}

	public static String shortJavaFileName(String className) {
		return shortClassName(className) + ".java";
	}

	public static String fullJavaFileName(String basePath, String className) {
		return getPackagePath(basePath, className) + File.separator
				+ shortJavaFileName(className);
	}

	public static String getPathFromFile(String fileName) {
		int index = fileName.lastIndexOf(File.separator);
		if (index < 0) {
			return ".";
		}
		return fileName.substring(0, index);
	}

	public static String findDepCfgFile(String genCfg, String className) {
		if ((genCfg == null) || ("".equals(genCfg)) || (className == null)
				|| ("".equals(className))) {
			return null;
		}

		String module = getPOModule(className);
		if (module == null) {
			return null;
		}

		File cfgFile = new File(genCfg);
		File path = cfgFile.getParentFile();
		if (path == null)
			return null;
		String schemaPath = path.getName();
		path = path.getParentFile();
		if (path == null)
			return null;
		path = path.getParentFile();
		if (path == null)
			return null;
		String srcPath = path.getAbsolutePath();

		String depCfg = srcPath + File.separator + module + File.separator
				+ schemaPath + File.separator + module + ".xml";
		path = new File(depCfg);
		if ((path.exists()) && (path.isFile())) {
			return depCfg;
		}
		return null;
	}

	public static BeanPropertyDef manyOneProperty(String className,
			String itemType, POConfigRepository poRepository) {
		if ((className == null) || (itemType == null)) {
			return null;
		}
		PersistObjectDescDef itemDesc = poRepository.getPOConfig(itemType);
		if (itemDesc == null) {
			return null;
		}

		BeanPropertyDef[] itemProps = itemDesc.getBeanPropertyDef();
		for (int i = 0; i < itemProps.length; ++i) {
			PropertyMappingDef mappingDef = itemProps[i]
					.getPropertyMappingDef();
			if ((mappingDef == null)
					|| (mappingDef.getPropertyMappingChoice() == null))
				continue;
			ManyOneAssociationDef manyOneDef = mappingDef
					.getPropertyMappingChoice().getManyOneAssociationDef();
			if ((manyOneDef != null)
					&& (className.equals(itemProps[i].getType()))) {
				return itemProps[i];
			}

		}

		return null;
	}

	public static BeanPropertyDef propertyDef(PersistObjectDescDef poDef,
			String propertyName) {
		if ((null == poDef) || (null == propertyName)) {
			return null;
		}
		return propDefByName(propertyName, poDef.getBeanPropertyDef());
	}

	public static BeanPropertyDef idPropertyDef(PersistObjectDescDef poDef) {
		if (null == poDef) {
			return null;
		}
		BeanPropertyDef[] props = poDef.getBeanPropertyDef();
		for (int i = 0; i < props.length; ++i) {
			PropertyMappingDef mappingDef = props[i].getPropertyMappingDef();
			if ((null != mappingDef)
					&& (null != mappingDef.getPropertyMappingChoice())
					&& (null != mappingDef.getPropertyMappingChoice()
							.getIdDef())) {
				return props[i];
			}
		}
		return null;
	}

	public static BeanPropertyDef[] embedIndexDef(PersistObjectDescDef poDef) {
		if (null == poDef) {
			return new BeanPropertyDef[0];
		}
		List propList = new ArrayList();
		EmbedObjectDef embedDef = poDef.getEmbedObjectDef();
		if (null != embedDef) {
			EmbedObjectItem[] embedItems = embedDef.getEmbedObjectItem();
			for (int i = 0; i < embedItems.length; ++i) {
				EmbedQueryItem[] queryItems = embedItems[i].getQueryMethod()
						.getEmbedQueryItem();
				for (int j = 0; j < queryItems.length; ++j) {
					QueryCondition condition = queryItems[j]
							.getQueryCondition();
					BeanPropertyDef propDef = propertyDef(poDef, condition
							.getPropertyName());
					if ((null == propDef) || (propList.contains(propDef)))
						continue;
					propList.add(propDef);
				}
			}

		}

		return ((BeanPropertyDef[]) propList.toArray(new BeanPropertyDef[0]));
	}

	public static void main(String[] args) {
		System.out.println("findDepCfgFile test:");
		System.out
				.println(findDepCfgFile(
						"D:\\project\\tjac-manage\\src\\server\\balance\\schema\\balance.xml",
						"com.leadron.test.user.po.PartnerPO"));
	}

	static {
		primaryMap = new HashMap();
		primaryMap.put("boolean", "java.lang.Boolean");
		primaryMap.put("char", "java.lang.Character");
		primaryMap.put("byte", "java.lang.Byte");
		primaryMap.put("short", "java.lang.Short");
		primaryMap.put("int", "java.lang.Integer");
		primaryMap.put("long", "java.lang.Long");
		primaryMap.put("float", "java.lang.Float");
		primaryMap.put("double", "java.lang.Double");
		primaryMap.put("String", "java.lang.String");
		primaryMap.put("Boolean", "java.lang.Boolean");
		primaryMap.put("Character", "java.lang.Character");
		primaryMap.put("Byte", "java.lang.Byte");
		primaryMap.put("Short", "java.lang.Short");
		primaryMap.put("Integer", "java.lang.Integer");
		primaryMap.put("Long", "java.lang.Long");
		primaryMap.put("Float", "java.lang.Float");
		primaryMap.put("Double", "java.lang.Double");
		primaryMap.put("java.lang.Boolean", "java.lang.Boolean");
		primaryMap.put("java.lang.Character", "java.lang.Character");
		primaryMap.put("java.lang.Byte", "java.lang.Byte");
		primaryMap.put("java.lang.Short", "java.lang.Short");
		primaryMap.put("java.lang.Integer", "java.lang.Integer");
		primaryMap.put("java.lang.Long", "java.lang.Long");
		primaryMap.put("java.lang.Float", "java.lang.Float");
		primaryMap.put("java.lang.Double", "java.lang.Double");
		primaryMap.put("java.lang.String", "java.lang.String");

		collectionMap = new HashMap();
		collectionMap.put("java.util.Set", "java.util.HashSet");
		collectionMap.put("java.util.Map", "java.util.HashMap");
		collectionMap.put("java.util.List", "java.util.ArrayList");
	}
}